package com.jeeplus.modules.cms.front.web;

import com.jeeplus.cms.common.CmsConstant;
import com.jeeplus.cms.util.CaptchaUtils;
import com.jeeplus.cms.util.CmsPage;
import com.jeeplus.cms.util.CmsUtils;
import com.jeeplus.cms.util.NetworkUtil;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.cms.column.entity.CmsColumn;
import com.jeeplus.modules.cms.column.service.CmsColumnService;
import com.jeeplus.modules.cms.content.entity.CmsContents;
import com.jeeplus.modules.cms.content.service.CmsContentsService;
import com.jeeplus.modules.cms.dm.service.CmsDynamicService;
import com.jeeplus.modules.cms.dyn.entity.CmsContentsDyn;
import com.jeeplus.modules.cms.dyn.service.CmsContentsDynService;
import com.jeeplus.modules.cms.guestbook.entity.CmsGuestbook;
import com.jeeplus.modules.cms.guestbook.service.CmsGuestbookService;
import com.jeeplus.modules.cms.guestbook.vo.CaptchaVo;
import com.jeeplus.modules.cms.model.entity.CmsModel;
import com.jeeplus.modules.cms.site.entity.CmsSite;
import com.jeeplus.modules.cms.site.service.CmsSiteService;
import com.jeeplus.modules.cms.theme.entity.CmsThemes;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/**
 * 网站Controller
 *
 * @author LiGuanHua
 * @version 2017-9-7
 */
@Controller
@RequestMapping(value = "${frontPath}/{siteId}")
public class FrontController extends BaseController {

    @Autowired
    private CmsColumnService columnService;
    @Autowired
    private CmsContentsService contentsService;
    @Autowired
    private CmsContentsDynService contentsDynService;
    @Autowired
    private CmsSiteService siteService;
    @Autowired
    private CmsDynamicService dynamicService;
    @Autowired
    private CmsGuestbookService guestbookService;

    @Autowired
    private freemarker.template.Configuration configuration;

    @ApiOperation("获取验证码")
    @ResponseBody
    @GetMapping(value = "/captcha")
    public AjaxJson captcha() throws IOException {
        return AjaxJson.success().put("data", CaptchaUtils.getCaptcha());
    }

    /**
     * 网站首页
     */
    @RequestMapping(value = {"", "index${urlSuffix}"})
    public String index(Model model, @PathVariable String siteId) {
        CmsSite site = CmsUtils.getSite(siteId);
        String index = site.getCustomIndexView();
        if (StringUtils.isBlank(index)) {
            index = CmsUtils.DEFAULT_INDEX_VIEW;
        }
        CmsColumn column = new CmsColumn();
        column.setId("0");
        siteService.updateHitsAddOne(siteId);
        addAttribute(model, siteId, column);
        //供前端页面在切换pageNo或单击栏目时连接请求URL，正常访问时为空。首页不设预览功能
        model.addAttribute("preview", "");
        return "themes/" + site.getTheme().getEnname() + "/" + index;
    }

    /**
     * 栏目入口
     */
    @RequestMapping(value = "col-{columnId}${urlSuffix}")
    public String column(@PathVariable String columnId, Model model,
                         @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                         @PathVariable String siteId, String contentTypeId) {
        CmsColumn column = CmsUtils.getColumnById(columnId);
        column = null == column ? new CmsColumn() : column;
        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column.getModel() ? column.getModel().getId() : "");
        column.setModel(cmsModel);

        if (!checkColumn(column, model, 1)) {
            return CmsUtils.ERROR_VIEW_404;
        }
        columnService.updateHitsAddOne(columnId);
        addAttribute(model, siteId, column);
        //供前端页面在切换pageNo或单击栏目时连接请求URL，正常访问时为空
        model.addAttribute("preview", "");
        model.addAttribute("contentTypeId", contentTypeId);

        return "forward:" + cmsModel.getDataModel() + "-" + columnId + "?requestOf=forward&isPreview=0&pageNo=" + pageNo;
    }

    /**
     * 详情入口
     */
    @RequestMapping(value = "view-{columnId}-{contentId}${urlSuffix}")
    public String view(@PathVariable String columnId, @PathVariable String contentId, Model model, @PathVariable String siteId, String contentTypeId) {
        // 获取栏目和页面模型
        CmsColumn column = CmsUtils.getColumnById(columnId);
        column = null == column ? new CmsColumn() : column;
        CmsModel cmsModel = CmsUtils.getModelById(null != column.getModel() ? column.getModel().getId() : "");
        column.setModel(cmsModel);
        if (!checkColumn(column, model, 2)) {
            return CmsUtils.ERROR_VIEW_404;
        }
        addAttribute(model, siteId, column);
        //供前端页面在切换pageNo或单击栏目时连接请求URL，正常访问时为空
        model.addAttribute("preview", "");
        model.addAttribute("contentTypeId", contentTypeId);

        return "forward:" + cmsModel.getDataModel() + "Form-" + columnId + "-" + contentId + "?requestOf=forward&isPreview=0";
    }

    /**
     * 视频播放
     */
    @RequestMapping(value = "video")
    public String video(String src, String poster, Model model, @PathVariable String siteId) {
        model.addAttribute("src", src);
        model.addAttribute("poster", poster);
        return "common/video";
    }

    /**
     * 栏目预览
     */
    @RequestMapping(value = "colPreview-{columnId}${urlSuffix}")
    public String colPreview(@PathVariable String columnId, Model model,
                             @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                             @PathVariable String siteId, String contentTypeId) {
        CmsColumn column = CmsUtils.getColumnById(columnId);
        column = null == column ? new CmsColumn() : column;
        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column.getModel() ? column.getModel().getId() : "");
        column.setModel(cmsModel);
        if (!checkColumn(column, model, 3)) {
            return "redirect:" + CmsUtils.MESSAGE_URL;
        }
        addAttribute(model, siteId, column);
        //供前端页面在切换pageNo或单击栏目时连接请求URL，正常访问时为空
        model.addAttribute("preview", "Preview");
        model.addAttribute("contentTypeId", contentTypeId);

        return "forward:" + cmsModel.getDataModel() + "-" + columnId + "?requestOf=forward&isPreview=1&pageNo=" + pageNo;
    }

    /**
     * 详情预览
     */
    @RequestMapping(value = "viewPreview-{columnId}-{contentId}${urlSuffix}")
    public String viewPreview(@PathVariable String columnId, @PathVariable String contentId, Model model, @PathVariable String siteId, String contentTypeId) {
        CmsColumn column = CmsUtils.getColumnById(columnId);
        column = null == column ? new CmsColumn() : column;
        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column.getModel() ? column.getModel().getId() : "");
        column.setModel(cmsModel);
        if (!checkColumn(column, model, 4)) {
            return "redirect:" + CmsUtils.MESSAGE_URL;
        }
        addAttribute(model, siteId, column);
        //供前端页面在切换pageNo或单击栏目时连接请求URL，正常访问时为空
        model.addAttribute("preview", "Preview");
        model.addAttribute("contentTypeId", contentTypeId);


        return "forward:" + cmsModel.getDataModel() + "Form-" + columnId + "-" + contentId + "?requestOf=forward&isPreview=1";
    }

    /*******************************************************************************************************************************/

    /**
     * 内容数据模型-列表
     */
    @RequestMapping(value = "contents-{columnId}", params = "requestOf=forward")
    public String contents(@PathVariable String columnId, Model model,
                           @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                           @RequestParam(required = false) String contentTypeId,
                           @RequestParam(required = false, defaultValue = "0") String isPreview, @PathVariable String siteId) {
        // 获取配置
        CmsColumn column = CmsUtils.getColumnById(columnId);
        CmsThemes theme = CmsUtils.getSiteTheme(siteId);

        String paramConfig = null != column ? column.getParamConfig() : "";
        paramConfig = org.apache.commons.lang3.StringUtils.defaultIfBlank(paramConfig, theme.getParamConfig());
        Integer pageSize = (Integer) CmsUtils.getParamConfig(CmsConstant.THEME_PARAM_PAGE_SIZE_KEY, paramConfig);

        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column && null != column.getModel() ? column.getModel().getId() : "");


        CmsContents contents = new CmsContents();
        contents.setColumnId(columnId);
        contents.setReleaseDate(new Date());
        contents.setContentTypeId(contentTypeId);
        if (CmsConstant.YES.equals(isPreview)) {
            //9表示可以查询0-发布、2-审核状态的记录
            contents.setDelFlag("9");
        } else {
            contents.setDelFlag(CmsContents.DEL_FLAG_NORMAL);
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CmsConstant.THEME_PARAM_PAGE_SIZE_VAL;
        }
        if (CmsConstant.NO.equals(cmsModel.getPageFlag())) {
            pageSize = CmsConstant.THEME_PARAM_NO_PAGE_SIZE_VAL;
        }
        model.addAttribute("pageFlag", cmsModel.getPageFlag());

        //更新过期权重
        contentsService.updateExpiredWeightToReq();
        Page<CmsContents> page = contentsService.findPage(new CmsPage<>(pageNo, pageSize), contents);
        model.addAttribute("page", page);
        model.addAttribute("contentTypeId",contentTypeId);

        return "themes/" + theme.getEnname() + "/" + cmsModel.getFrontList();
    }

    /**
     * 内容数据模型-详情
     */
    @RequestMapping(value = "contentsForm-{columnId}-{contentId}", params = "requestOf=forward")
    public String contentsForm(@PathVariable String columnId, @PathVariable String contentId, Model model,
                               @RequestParam(required = false) String isPreview, @PathVariable String siteId) {
        String messageType = "";
        String messageBody = "";
        CmsColumn column = CmsUtils.getColumnById(columnId);
        CmsThemes theme = CmsUtils.getSiteTheme(siteId);
        CmsContents contents = null;
        if (StringUtils.isBlank(contentId)) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "找不到发布内容信息，内容编号不能为空！";
        } else if ((contents = CmsUtils.getContentById(contentId)) == null || CmsContents.DEL_FLAG_DELETE.equals(contents.getDelFlag())) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "发布内容信息已删除，不能访问或预览！";
        } else if (!CmsConstant.YES.equals(isPreview) && CmsContents.DEL_FLAG_AUDIT.equals(contents.getDelFlag())) {
            //不影响预览
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "发布内容信息正在审核，不能访问！";
        } else if (!CmsConstant.YES.equals(isPreview) && (contents.getReleaseDate() == null || contents.getReleaseDate().compareTo(new Date()) > 0)) {
            //不影响预览
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "发布内容信息未到发布时间，不能访问！";
        }

        if (null == theme) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "未找到对应的站点主题";
        }

        if (StringUtils.isNotBlank(messageBody)) {
            model.addAttribute("messageType", messageType);
            model.addAttribute("messageBody", messageBody);
            if (CmsConstant.YES.equals(isPreview)) {
                return "redirect:" + CmsUtils.MESSAGE_URL;
            } else {
                return CmsUtils.ERROR_VIEW_404;
            }
        }
        if (!CmsConstant.YES.equals(isPreview)) {
            //点击数增加1
            contentsService.updateHitsAddOne(contents.getId());
        }
        model.addAttribute("contents", contents);

        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column && null != column.getModel() ? column.getModel().getId() : "");
        return "themes/" + theme.getEnname() + "/" + cmsModel.getFrontForm();
    }

    /**
     * 动态内容数据模型-列表
     */
    @RequestMapping(value = "contentsDyn-{columnId}", params = "requestOf=forward")
    public String contentsDyn(@PathVariable String columnId, Model model,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                              @RequestParam(required = false, defaultValue = "0") String isPreview, @PathVariable String siteId) {
        CmsThemes theme = CmsUtils.getSiteTheme(siteId);
        CmsColumn column = CmsUtils.getColumnById(columnId);

        String paramConfig = null != column ? column.getParamConfig() : "";
        paramConfig = org.apache.commons.lang3.StringUtils.defaultIfBlank(paramConfig, theme.getParamConfig());
        Integer pageSize = (Integer) CmsUtils.getParamConfig(CmsConstant.THEME_PARAM_PAGE_SIZE_KEY, paramConfig);


        CmsContentsDyn contentsDyn = new CmsContentsDyn();
        CmsModel cmsModel = CmsUtils.getModelById(null != column && null != column.getModel() ? column.getModel().getId() : "");
        contentsDyn.setColumn(column);
        contentsDyn.setReleaseDate(new Date());
        if (CmsConstant.YES.equals(isPreview)) {
            //9表示可以查询0-发布、2-审核状态的记录
            contentsDyn.setDelFlag("9");
        } else {
            contentsDyn.setDelFlag(CmsContents.DEL_FLAG_NORMAL);
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CmsConstant.THEME_PARAM_PAGE_SIZE_VAL;
        }

        if (CmsConstant.NO.equals(cmsModel.getPageFlag())) {
            pageSize = CmsConstant.THEME_PARAM_NO_PAGE_SIZE_VAL;
        }
        model.addAttribute("pageFlag", cmsModel.getPageFlag());

        // 更新过期权重
        contentsDynService.updateExpiredWeightToReq();
        Page<CmsContentsDyn> page = contentsDynService.findPage(new CmsPage<>(pageNo, pageSize), contentsDyn);

        // 反序列化动态表单数据
        for (int i = 0; i < page.getList().size(); i++) {
            contentsDyn = page.getList().get(i);
            dynamicService.modelFromJson(contentsDynService.getDynamic(contentsDyn).getDataModel(), contentsDyn.getMainMap(), contentsDyn.getTypeMap());
            contentsDyn.setDataMap(dynamicService.dataFromJson(contentsDyn.getData(), contentsDyn.getTypeMap()));
            if (null == contentsDyn.getDataMap()) {
                contentsDyn.setDataMap(new HashMap<>(16));
            }
        }


        model.addAttribute("page", page);
        return "themes/" + theme.getEnname() + "/" + cmsModel.getFrontList();
    }

    /**
     * 动态内容数据模型-详情
     */
    @RequestMapping(value = "contentsDynForm-{columnId}-{contentId}", params = "requestOf=forward")
    public String contentsDynForm(@PathVariable String columnId, @PathVariable String contentId, Model model,
                                  @RequestParam(required = false) String isPreview, @PathVariable String siteId) {
        String messageType = "";
        String messageBody = "";
        CmsColumn column = CmsUtils.getColumnById(columnId);
        CmsThemes theme = CmsUtils.getSiteTheme(siteId);
        CmsContentsDyn contentsDyn = null;
        if (StringUtils.isBlank(contentId)) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "找不到发布内容信息，内容编号不能为空！";
        } else if ((contentsDyn = CmsUtils.getContentDynById(contentId)) == null || CmsContentsDyn.DEL_FLAG_DELETE.equals(contentsDyn.getDelFlag())) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "发布内容信息已删除，不能访问或预览！";
        } else if (!CmsConstant.YES.equals(isPreview) && CmsContentsDyn.DEL_FLAG_AUDIT.equals(contentsDyn.getDelFlag())) {
            //不影响预览
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "发布内容信息正在审核，不能访问！";
        } else if (!CmsConstant.YES.equals(isPreview) && (contentsDyn.getReleaseDate() == null || contentsDyn.getReleaseDate().compareTo(new Date()) > 0)) {
            //不影响预览
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "发布内容信息未到发布时间，不能访问！";
        }
        if (StringUtils.isNotBlank(messageBody)) {
            model.addAttribute("messageType", messageType);
            model.addAttribute("messageBody", messageBody);
            if (CmsConstant.YES.equals(isPreview)) {
                return "redirect:" + CmsUtils.MESSAGE_URL;
            } else {
                return CmsUtils.ERROR_VIEW_404;
            }
        }
        if (!CmsConstant.YES.equals(isPreview)) {
            //点击数增加1
            contentsDynService.updateHitsAddOne(contentsDyn.getId());
        }

        //反序列化动态表单模型
        dynamicService.modelFromJson(contentsDynService.getDynamic(contentsDyn).getDataModel(), contentsDyn.getMainMap(), contentsDyn.getTypeMap());
        contentsDyn.setDataMap(dynamicService.dataFromJson(contentsDyn.getData(), contentsDyn.getTypeMap()));
        if (null == contentsDyn.getDataMap()) {
            contentsDyn.setDataMap(new HashMap<>(16));
        }

        model.addAttribute("contentsDyn", contentsDyn);

        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column && null != column.getModel() ? column.getModel().getId() : "");
        return "themes/" + theme.getEnname() + "/" + cmsModel.getFrontForm();
    }

    /**
     * 留言本数据模型-列表
     */
    @RequestMapping(value = "guestbook-{columnId}", params = "requestOf=forward")
    public String guestbook(@PathVariable String columnId, Model model,
                            @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                            @RequestParam(required = false, defaultValue = "0") String isPreview, @PathVariable String siteId) {
        CmsThemes theme = CmsUtils.getSiteTheme(siteId);
        CmsColumn column = CmsUtils.getColumnById(columnId);
        String paramConfig = null != column ? column.getParamConfig() : "";
        paramConfig = org.apache.commons.lang3.StringUtils.defaultIfBlank(paramConfig, theme.getParamConfig());
        Integer pageSize = (Integer) CmsUtils.getParamConfig(CmsConstant.THEME_PARAM_GUESTBOOK_COUNT_KEY, paramConfig);
        if (pageSize == null || pageSize == 0) {
            pageSize = (Integer) CmsUtils.getParamConfig(CmsConstant.THEME_PARAM_PAGE_SIZE_KEY, org.apache.commons.lang3.StringUtils.defaultIfBlank(column.getParamConfig(), theme.getParamConfig()));
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = CmsConstant.THEME_PARAM_PAGE_SIZE_VAL;
        }

        CmsGuestbook guestbook = new CmsGuestbook();
        guestbook.setColumn(column);
        guestbook.setOpenFlag(CmsConstant.YES);
        if (CmsConstant.YES.equals(isPreview)) {
            //9表示可以查询0-发布、2-审核状态的记录
            guestbook.setDelFlag("9");
        } else {
            guestbook.setDelFlag(CmsGuestbook.DEL_FLAG_NORMAL);
        }

        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column && null != column.getModel() ? column.getModel().getId() : "");
        if (CmsConstant.NO.equals(cmsModel.getPageFlag())) {
            pageSize = CmsConstant.THEME_PARAM_NO_PAGE_SIZE_VAL;
        }
        model.addAttribute("pageFlag", cmsModel.getPageFlag());

        //更新过期权重
        guestbookService.updateExpiredWeightToReq();
        CmsGuestbook evaluGuestbook = guestbookService.getEvalu(guestbook);
        Page<CmsGuestbook> page = guestbookService.findPage(new CmsPage<>(pageNo, pageSize), guestbook);
        model.addAttribute("page", page);
        model.addAttribute("inputGuestbook", new CmsGuestbook());
        model.addAttribute("evaluGuestbook", evaluGuestbook);

        return "themes/" + theme.getEnname() + "/" + cmsModel.getFrontList();
    }

    /**
     * 留言本数据模型-详情
     */
    @RequestMapping(value = "guestbookForm-{columnId}-{contentId}", params = "requestOf=forward")
    public String guestbookForm(@PathVariable String columnId, @PathVariable String contentId, Model model,
                                @RequestParam(required = false) String isPreview, @PathVariable String siteId) {
        String messageType = "";
        String messageBody = "";
        CmsColumn column = CmsUtils.getColumnById(columnId);
        CmsThemes theme = CmsUtils.getSiteTheme(siteId);
        CmsGuestbook guestbook = null;
        if (StringUtils.isBlank(contentId)) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "找不到留言本信息，留言本编号不能为空！";
        } else if ((guestbook = guestbookService.get(contentId)) == null || CmsGuestbook.DEL_FLAG_DELETE.equals(guestbook.getDelFlag())) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "留言本信息已删除，不能访问或预览！";
        } else if (!CmsConstant.YES.equals(isPreview) && CmsGuestbook.DEL_FLAG_AUDIT.equals(guestbook.getDelFlag())) {
            //不影响预览
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "留言本信息正在审核，不能访问！";
        } else if (CmsConstant.NO.equals(guestbook.getOpenFlag())) {
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "留言本信息不公开，不能访问！";
        }
        if (StringUtils.isNotBlank(messageBody)) {
            model.addAttribute("messageType", messageType);
            model.addAttribute("messageBody", messageBody);
            if (CmsConstant.YES.equals(isPreview)) {
                return "redirect:" + CmsUtils.MESSAGE_URL;
            } else {
                return CmsUtils.ERROR_VIEW_404;
            }
        }
        model.addAttribute("guestbook", guestbook);

        // 获取页面模型
        CmsModel cmsModel = CmsUtils.getModelById(null != column && null != column.getModel() ? column.getModel().getId() : "");
        return "themes/" + theme.getEnname() + "/" + cmsModel.getFrontForm();
    }


    /**
     * 保存留言表
     */
    @RequestMapping("/guestbook/save")
    @ResponseBody
    public AjaxJson save(@PathVariable String siteId, CmsGuestbook cmsGuestbook, CaptchaVo captchaVo, HttpServletRequest request) throws Exception {
        if (!CaptchaUtils.validateCaptcha(captchaVo)) {
            return AjaxJson.error("验证码错误");
        }

        // 设置客户端ip和所属站点
        cmsGuestbook.setIp(NetworkUtil.getIpAddress(request));
        cmsGuestbook.setSiteId(siteId);

        // 校验
        String errMsg = beanValidator(cmsGuestbook);
        if (StringUtils.isNotBlank(errMsg)) {
            return AjaxJson.error(errMsg);
        }

        // 新增或编辑表单保存
        guestbookService.save(cmsGuestbook);
        return AjaxJson.success("保存留言表成功");
    }

    /*******************************************************************************************************************************/

    /**
     * 栏目校验
     *
     * @param column
     * @param model
     * @param type   1-栏目访问验证；2-详情访问验证；3-栏目预览验证；4-详情预览验证
     * @return
     */
    private Boolean checkColumn(CmsColumn column, Model model, int type) {
        CmsModel cmsModel = column.getModel();
        String messageType = "";
        String messageBody = "";
        if (StringUtils.isBlank(column.getId())) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "找不到栏目信息，栏目编号不能为空！";
        } else if (!CmsColumn.DEL_FLAG_NORMAL.equals(column.getDelFlag())) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "栏目已经删除或正在审核！";
        } else if (column.getHasChildren()) {
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "此栏目存在下级栏目，不能访问或预览！";
        } else if ((type == 1 || type == 2) && CmsConstant.NO.equals(column.getEnableFlag())) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "栏目没有启用！";
        } else if (StringUtils.isBlank(cmsModel.getDataModel())) {
            messageType = CmsUtils.MESSAGE_TYPE_DANGER;
            messageBody = "栏目所属模型的数据模型为空，请重新维护！";
        } else if (CmsUtils.CONTENT_NONE_URL.equals(cmsModel.getDataModel())) {
            messageType = CmsUtils.MESSAGE_TYPE_INFO;
            messageBody = "此栏目无发布内容（类似链接模型），不能访问或预览！";
        } else if ((type == 1 || type == 3) && (StringUtils.isBlank(cmsModel.getFrontList()) || CmsUtils.CONTENT_NONE_URL.equals(cmsModel.getFrontList()))) {
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "没有找到对应的列表页面，请确认访问或预览参数！";
        } else if ((type == 2 || type == 4) && (StringUtils.isBlank(cmsModel.getFrontForm()) || CmsUtils.CONTENT_NONE_URL.equals(cmsModel.getFrontForm()))) {
            messageType = CmsUtils.MESSAGE_TYPE_WARNING;
            messageBody = "没有找到对应的详情页面，请确认访问或预览参数！";
        }

        if (StringUtils.isNotBlank(messageBody)) {
            model.addAttribute("messageType", messageType);
            model.addAttribute("messageBody", messageBody);
            return false;
        }
        return true;
    }


    /**
     * 设置属性
     */
    private void addAttribute(Model model, String siteId, CmsColumn column) {
        String currentMainColumnId = "";
        //当前栏目
        model.addAttribute(CmsUtils.CURRENT_COLUMN_ATTRIBUTE, column);
        if ("0".equals(column.getId())) {
            //当前主栏目id-首页
            currentMainColumnId = "0";
        } else {
            //当前主栏目id
            currentMainColumnId = "0".equals(column.getParent().getId()) ? column.getId() : column.getParentIds().split(",")[1];
            //栏目参数配置
            CmsUtils.addParamConfigAttribute(model, column);
        }
        model.addAttribute(siteId, siteId);
        model.addAttribute(CmsUtils.CURRENT_MAIN_COLUMNID_ATTRIBUTE, currentMainColumnId);
        //增加栏目参数配置
        CmsUtils.addParamConfigAttribute(model, CmsUtils.COLUMN_PARAM_CONFIG_PREFIX, column.getParamConfig());

        // 增加主题参数配置，主要用于辅助提示一些图片的推荐尺寸、栏目显示个数等
        CmsThemes theme = CmsUtils.getSiteTheme(siteId);
        model.addAttribute("theme", null == theme ? "" : theme.getEnname());
        CmsUtils.addParamConfigAttribute(model, CmsUtils.THEMES_PARAM_CONFIG_PREFIX, null == theme ? null : theme.getParamConfig());
    }

}
